﻿using System;
using System.Diagnostics;

namespace LaoLuo
{
    /// <summary>
    /// 提供统计方法
    /// </summary>
    public static class Statistics
    {
        /// <summary>
        /// get the time-cost of an action being called several times
        /// </summary>
        /// <param name="action">action to be called</param>
        /// <param name="executionCount">number of times the action should be called</param>
        /// <returns>the time-cost</returns>
        public static TimeSpan TimeCost(Action action, long executionCount = 1)
        {
            var ret = TimeSpan.Zero;

            for (var i = 0; i < executionCount; i++)
            {
                var s = Stopwatch.StartNew();

                action();

                s.Stop();

                ret += s.Elapsed;
            }

            return ret;
        }
    }
}
